package com.example.blogother.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.example.blogother.mapper.BadwordMapper;
import com.xpd.blog.bean.Badword;
import com.xpd.blog.bean.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xpd
 * @since 2024-10-09
 */
@RestController
@RequestMapping("/badword")
public class BadwordController {
    @Autowired
    BadwordMapper badwordMapper;
    @RequestMapping("getAllBadWord")

    public Result getAllBadWord(@RequestParam int page, @RequestParam int size, @RequestBody Badword badword){
        Page p=new PageDTO(page,size);
        LambdaQueryWrapper<Badword> lqw=new LambdaQueryWrapper<>();
        if(badword.getBname()!=null&&!"".equals(badword.getBname())){
            lqw.like(Badword::getBname,badword.getBname());
        }
        System.out.println(badword.getLevel());
        if(badword.getLevel()!=null&&!"".equals(badword.getLevel())){
            lqw.like(Badword::getLevel,badword.getLevel());
            System.out.println("我执行了");
        }
        Page page1 = badwordMapper.selectPage(p, lqw);
        return new Result(1,"查询成功",page1);
    }

    //新增
    @RequestMapping("addBadWord")
    public Result addBadWord(@RequestBody Badword badword){
        if(badword.getLevel()==null||"".equals(badword.getLevel())){
            badword.setLevel("低");
        }
        if(badword.getBname()==null||"".equals(badword.getBname())){
            return new Result(-2,"敏感词不能为空",null);
        }else{
            LambdaQueryWrapper<Badword> lqw=new LambdaQueryWrapper<>();
            lqw.eq(Badword::getBname,badword.getBname());
            boolean exists = badwordMapper.exists(lqw);
            if(exists){
                return new Result(-2,"该敏感词已经存在了，不能重复添加",null);
            }else{
                badword.setStatus(1);
                int insert = badwordMapper.insert(badword);
                if(insert>0){
                    return new Result(1,"新增成功",null);
                }else{
                    return new Result(-1,"新增失败",null);
                }
            }
        }
    }


    //删除
    @RequestMapping("delBadWord")
    public Result delBadWord(@RequestParam int bid,@RequestParam int status){
        LambdaQueryWrapper<Badword> lqw=new LambdaQueryWrapper<>();
        lqw.eq(Badword::getBid,bid);
        Badword badword = badwordMapper.selectOne(lqw);
        badword.setStatus(status);
        int update = badwordMapper.update(badword, lqw);
        if(update>0){
            return new Result(1,"操作成功",null);
        }else{
            return new Result(-1,"操作失败",null);
        }
    }

    //修改
    @RequestMapping("updateBadWord")
    public Result updateBadWord(@RequestBody Badword badword){
        LambdaQueryWrapper<Badword> lqw=new LambdaQueryWrapper<>();
        lqw.eq(Badword::getBid,badword.getBid());
        int update = badwordMapper.update(badword, lqw);
        if(update>0){
            return new Result(1,"修改成功",null);
        }else{
            return new Result(-1,"修改失败",null);
        }
    }


}

